package com.xerokhan.diary.util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import com.xerokhan.database.Database;
import com.xerokhan.database.Table;
import com.xerokhan.generator.Generator;

public class DBGenerator {

	/**
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		Database.getConnection("sakila");
		generateContacts();
	}

	public static void address() throws SQLException {

		Table table = new Table("address");
		String[] cols = { "address", "phone" };
		ArrayList<HashMap<String, String>> records = table.getAllRecords(cols);
		ArrayList<String> lines = new ArrayList<String>();
		for (int i = 0; i < records.size(); i++) {
			lines.add(records.get(i).get(cols[0]) + "," + records.get(i).get(cols[1]));
		}

		Database.getConnection("world");
		table = new Table("city");
		records = table
				.getRecords("select city.Name as city, city.District as district, country.Name as country from city inner join country on city.CountryCode=country.Code;");

		for (int i = 0; i < lines.size(); i++) {
			HashMap<String, String> map = records.get(Math.abs(new Random().nextInt() % records.size()));
			System.out.println(lines.get(i) + "," + records.get(i).get("city") + "," + records.get(i).get("district") + "," + records.get(i).get("country"));
		}

	}

	public static void userGen() {
		for (int i = 0; i < GeneratorData.FIRST_NAME.length; i++) {
			String s = GeneratorData.FIRST_NAME[i].toLowerCase() + "," + GeneratorData.LAST_NAME[i].toLowerCase() + ","
					+ Generator.nextEmail(GeneratorData.FIRST_NAME[i], GeneratorData.LAST_NAME[i]).toLowerCase() + "," + GeneratorData.DOB[i] + ","
					+ Generator.nextGender() + "," + GeneratorData.DATE_OF_JOINING[i];
			System.out.println(s);
		}
	}

	public static void userNumberGenerator() {
		System.out.println("{");
		for (int i = 0; i < GeneratorData.DATE_OF_JOINING.length; i++) {
			String[] d = GeneratorData.DATE_OF_JOINING[i].split("-");
			System.out.println(",\"" + d[2] + d[0] + d[1] + i + "\"");
		}
		System.out.println("}");
	}

	public static void datesGenerator() {
		System.out.println("{");
		for (int i = 0; i < GeneratorData.FIRST_NAME.length; i++) {
			System.out.println(",\"" + Generator.nextDate(1950, 60) + "\"");
		}
		System.out.println("}");

		System.out.println("{");
		for (int i = 0; i < GeneratorData.FIRST_NAME.length; i++) {
			System.out.println(",\"" + Generator.nextDate(2010, 1) + "\"");
		}
		System.out.println("}");

	}

	public static void recordGenerator() throws SQLException {
		Table table = new Table("actor");
		String[] cols = { "first_name", "last_name" };
		ArrayList<HashMap<String, String>> records = table.getAllRecords(cols);

		System.out.println("{");
		for (int i = 0; i < records.size(); i++) {
			System.out.println(",\"" + records.get(i).get(cols[0]) + "\"");
		}
		System.out.println("}");
		System.out.println("{");

		for (int i = 0; i < records.size(); i++) {
			System.out.println(",\"" + records.get(i).get(cols[1]) + "\"");
		}
		System.out.println("}");
		System.out.println("{");
		for (int i = 0; i < records.size(); i++) {
			System.out.println(",\"\"");
		}
		System.out.println("}");
	}

	public static void generateContacts() {
		for (int i = 0; i < GeneratorData.FIRST_NAME.length; i++) {
			String type = GeneratorData.RELATIONSHIP[Math.abs(new Random().nextInt() % GeneratorData.RELATIONSHIP.length)];
			String email = GeneratorData.FIRST_NAME[i] + "." + GeneratorData.LAST_NAME[i] + "@email.com";
			String q = "insert into contact (user_id, type,timestamp, first_name,last_name,email, phone, mobile) values(1,'" + type + "','"
					+ new Date().getTime() + "','" + GeneratorData.FIRST_NAME[i] + "','" + GeneratorData.LAST_NAME[i] + "','" + email.toLowerCase() + "','(+91) 80 "
					+ GeneratorData.USER_NUMBER[i] + "','(+91) 98" + GeneratorData.USER_NUMBER[i] + "');";
			System.out.println(q);

		}
	}
}
